<?php
class UserCommentsController extends AppController{
	var $name = 'UserComments';
	var $uses = array('UserComment','UserImageBoard','Profile','UserLike');
	var $helpers = array('Html','Session');
	var $components = array('Session');
	function comments($user_image_board_id){

		$image = $this->UserImageBoard->findById($user_image_board_id);
			$user = $this->Profile->findByUser_id($image['UserImageBoard']['user_id']);
			$image['Profile'] = $user['Profile'];
			
			$like = $this->UserLike->find('count',array(
														'conditions'=>array(
															'UserLike.user_image_board_id'=>$image['UserImageBoard']['id'],
															'UserLike.user_id'=>$this->Auth->user('id'))
														));
			if($like > 0){
				$image['UserLike'] = true;
			}else{
				$image['UserLike'] = false;
			}

			$comment = $this->UserComment->find('all',array(
														  'conditions'=>array(
														  	'UserComment.user_image_board_id'=>$image['UserImageBoard']['id'])));
			$image['UserComment'] = $comment;

			$this->set('image',$image);

		
		/*$user_id=$this->Auth->user('id');
		//pr($user_id);
		$this->set('auth',$user_id);
		$a=$this->UserImageBoard->find('all',array('conditions'=>array('UserImageBoard.id'=>13)));
		//pr($a);
		$this->set('results',$a);*/
		$user_id=$this->Auth->user('id');
		$b=$this->UserComment->find('all',array('conditions'=>array('user_image_board_id'=>$user_image_board_id)));
		//$b=$this->UserComment->find('all');
		//pr($b);
		
		for($i=0; $i<count($b); $i++){
			$user=$this->Profile->findByUser_id($b[$i]['User']['id']);
			$b[$i]['Profile'] = $user['Profile'];
			
			
		}
		//($b);
		$this->set('usercomments',$b);
		
		if(!empty($this->data)&&$this->data['UserComment']['comment']!=null){
			//pr($this->data);
			$data=array('UserComment'=>array(
											'user_id'=>$user_id,
											'user_image_board_id'=>$user_image_board_id,
											'content'=>$this->data['UserComment']['comment']));
			//pr($data);
			$this->UserComment->save($data);
			$this->redirect(array('action'=>'comments',$user_image_board_id));
		}

	}
	function edit($usercomment){
		if(empty($this->data['UserComment']['newcomment'])){
			$this->data=$this->UserComment->read(NULL,$usercomment);
		}else{
			$a=$this->UserComment->find('all',array('conditions'=>array('UserComment.id'=>$usercomment)));
			//pr($a);
			$data=array('UserComment'=>array(
											'id'=>$usercomment,
											'user_id'=>$a[0]['UserComment']['user_id'],
											'user_image_board_id'=>$a[0]['UserComment']['user_image_board_id'],
											'content'=>$this->data['UserComment']['newcomment']));
			if($this->UserComment->save($data)){
				$this->redirect(array('action'=>'comments',$a[0]['UserComment']['user_image_board_id']));
			}
		}

	}
	function delete($usercomment){
		pr($usercomment);
		$id=$this->UserComment->findById($usercomment);
		pr($id);
		$this->UserComment->delete($usercomment);
		$this->redirect(array('action'=>'comments',$id['UserComment']['user_image_board_id']));
	}
}
?>